package com.xushu.learn_springboot_xushu.controller;

import com.xushu.learn_springboot_xushu.pojo.ResponseMessage;
import com.xushu.learn_springboot_xushu.pojo.User;
import com.xushu.learn_springboot_xushu.pojo.dto.UserDto;
import com.xushu.learn_springboot_xushu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: yu
 * @description:
 * @create: 2025/03/17 11:47
 */

@RestController     //接口方法返回对象 转化成json文本
@RequestMapping ("/user")       //localhost:8088/user/**
public class UserController {

    @Autowired
    IUserService userService;
    //REST
    //增加
    @PostMapping        //前端请求地址  URL: localhost:8088/user   method:post
    public ResponseMessage add(@Validated @RequestBody UserDto user) {
        User userAdd = userService.add(user);
        /*return "success!";*/
        return ResponseMessage.success(userAdd);
    }


    // 查询\
    @GetMapping("/{userId}")    // URL: localhost:8088/user/6    method: get
    public ResponseMessage get(@PathVariable Integer userId){
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }

    // 修改
    @PutMapping
    public ResponseMessage edit(@Validated @RequestBody UserDto user){
        User userNew = userService.edit(user);
        return ResponseMessage.success(userNew);
    }

    // 删除用户
    @DeleteMapping("/{userId}")
    public ResponseMessage delete(@PathVariable Integer userId){
        userService.delete(userId);
        return ResponseMessage.success();
    }


}
